package com.dtstack.jlogstash.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dtstack.jlogstash.util.alibaba.Bytes;

import oracle.sql.BLOB;
import oracle.sql.CLOB;

/**
 * Blob、Clob转byte[]工具类
 * @author zxb
 * @version 1.0.0
 *          2017年03月24日 22:00
 * @since Jdk1.6
 */
public class BlobClobUtil {

    private static Logger logger = LoggerFactory.getLogger(BlobClobUtil.class);

    public static byte[] convertBlob2Bytes(Blob blob) {
        if(blob == null)
            return null;
        InputStream inputStream = null;
        ByteArrayOutputStream baos = null;
        try {
            inputStream = blob.getBinaryStream();
            baos = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, baos, new byte[2048]);
            return baos.toByteArray();
        } catch (SQLException e) {
            logger.error("convertBlob2Bytes error!", e);
        } catch (IOException e) {
            logger.error("convertBlob2Bytes error!", e);
        } finally {
            IOUtils.closeQuietly(baos);
            IOUtils.closeQuietly(inputStream);
        }
        return null;
    }

    public static String convertClob2String(Clob clob) {
        if(clob == null)
            return null;

        Reader reader = null;
        try {
            reader = clob.getCharacterStream();
            int len = 2048;
            char[] charBuf = new char[len];

            StringWriter stringWriter = new StringWriter();
            try {
                while ((len = reader.read(charBuf)) != -1) {
                    stringWriter.write(charBuf, 0, len);
                }
                return stringWriter.toString();
            } catch (IOException e) {
                logger.error("convertClob2String", e);
            } finally {
                try {
                    stringWriter.close();
                } catch (IOException e) {
                    logger.error("close string writer error!", e);
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        logger.error("close reader error!", e);
                    }
                }
            }
        } catch (SQLException e) {
            logger.error("convertClob2String error!", e);
        }
        return null;
    }

    public static String getBodyString(BLOB blob) {
        try {
//            BLOB blob = (BLOB) (body);
            if (blob != null && blob.length() > 0) {
//                    byte[] content = ;
                return new String(blob.getBytes(1, (int) blob.length()), "GBK");
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取body的字符串 ，并解压
     * @param object  Body值
     * @return
     */
    public static String getBodyString(Object object) {
        byte[] body = null;
        String bodyStr = null;
        try {
            if(object instanceof byte []){
                body = (byte[]) object;
            }else if(object instanceof String){
                bodyStr = (String) object;
            }else if(object instanceof BLOB){
                bodyStr= getBodyString((BLOB) object);
            }

            
//            else {
//
//                bodyStr = getBodyString((Blob) object);
//            }
            if(bodyStr == null&&body!=null) {
                try{
                    bodyStr = new String(Bytes.unzip(body), "GBK");
                }catch (Exception e) {
                    bodyStr = new String(body,"GBK");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bodyStr;
    }

    /**
     * 将blob转化为byte[],可以转化二进制流的
     * @param blob
     * @return
     * @throws Exception
     */
    public static byte[] blobToBytes(Blob blob) throws Exception {
        InputStream is = null;
        byte[] b = null;
        try {
            is = blob.getBinaryStream();
            b = new byte[(int) blob.length()];
            is.read(b);
            return b;
        } catch (Exception e) {
//            e.printStackTrace();
            throw e;
        } finally {
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
